package www.bplead.com.printer.service;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.zj.btsdk.BluetoothService;
import com.zj.btsdk.PrintPic;

/**
 * Created by wangyangzi on 2016/10/15.
 */
public class PrinterService {
    private static final String TAG = "PrintService";

    //使用打印功能先创建蓝牙服务
    private static BluetoothService bs;
    private void connect(String address){
        //蓝牙服务未创建那么创建
        if(bs == null){
            bs = new BluetoothService(null, mHandler);
        }
        //蓝牙未连接那么建立连接
        if(bs.getState() != BluetoothService.STATE_CONNECTED){
            BluetoothDevice con_dev = bs.getDevByMac(address);
            bs.connect(con_dev);
        }
    }

    //停止蓝牙服务
    public static void stop(){
        if(bs != null){
            bs.stop();
            bs = null;
        }
    }

    private Handler printPageHandler = null;
    private void sendMsgToPringPage(int msg){
        if(printPageHandler != null){
            printPageHandler.sendEmptyMessage(msg);
        }
    }

    /**
     * 创息建一个Handler实例，用于接收BluetoothService类返回回来的消
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int status = -1;
            switch (msg.what) {
                case BluetoothService.MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED:   //已连接
                            Log.d("蓝牙调试", "已连接！");
                            status = 1;
                            break;
                        case BluetoothService.STATE_CONNECTING:  //正在连接
                            Log.d("蓝牙调试", "正在连接.....");
                            status = 0;
                            break;
                        case BluetoothService.STATE_LISTEN:     //监听连接的到来
                        case BluetoothService.STATE_NONE:
                            Log.d("蓝牙调试","等待连接.....");
                            break;
                    }
                    break;
                case BluetoothService.MESSAGE_CONNECTION_LOST:    //蓝牙已断开连接
                    Log.d("蓝牙调试", "连接已断开.....");
                    status = 2;
                    break;
                case BluetoothService.MESSAGE_UNABLE_CONNECT:     //无法连接设备
                    Log.d("蓝牙调试", "无法连接设备.....");
                    status = 2;
                    break;
            }
            sendMsgToPringPage(status);
        }
    };

    public String printImage(Context context, Handler handler, Bitmap bm){
        SharedPreferences preferences = context.getSharedPreferences("current_device", context.MODE_PRIVATE);
        String currentPrintDevice = preferences.getString("current_print_device", "error");
        String bpd = preferences.getString("bluetooth_print_device_info","error");
        //未连接先建立连接
        if(bs == null || bs.getState() != BluetoothService.STATE_CONNECTED){
            if(!"error".equals(bpd)){
                String address = bpd.substring(0, bpd.indexOf("#"));
                connect(address);
                this.printPageHandler = handler;
            }
            return "no";
        }
        //已连接则直接打印
        printImage(bm);
        return "ok";
    }

    //打印图形
    public static void printImage(Bitmap bm) {
        byte[] sendData = null;
        PrintPic pg = new PrintPic();
        pg.initCanvas(608);
        pg.initPaint();
        pg.canvas.drawBitmap(bm, 0, 0,null);
        if (pg.length < 0 + bm.getHeight())
            pg.length = (0 + bm.getHeight());
        //pg.drawImage(0, 0, "/mnt/sdcard/123.jpg");
        sendData = pg.printDraw();
        bs.write(sendData);   //打印byte流数据
    }
}
